<?php
/**
 * 新人特惠管理
 *
 *
 *
 *
 * @copyright  Copyright (c) 中国追溯
 * @since      File available since Release v1.1
 */

defined('InShopNC') or exit('Access Invalid!');

class p_kanjia_goodsModel extends Model {

    const KANJIA_STATE_UNREVIEWED   = 0;    //默认：未审核
    const KANJIA_STATE_PASS         = 1;    //通过
    const KANJIA_STATE_FAIL         = 2;    //未通过
    const KANJIA_STATE_REAPPLY      = 3;    //再次申请
    const KANJIA_STATE_CLOSE        = 4;    //活动关闭
    const KANJIA_STATE_DELETE_ADMIN = 5;    //管理员删除
    const KANJIA_STATE_SOFT_DELETE  = 6;    //软删除

    const   STORE_GOODS_LIMIT      = 5;     //默认活动单一店铺最多可以报名商品数（0：不限制）

    private $mansong_state_array = array(
        self::KANJIA_STATE_UNREVIEWED   => '未审核',
        self::KANJIA_STATE_PASS         => '通过',
        self::KANJIA_STATE_FAIL         => '未通过',
        self::KANJIA_STATE_REAPPLY      => '再次申请',
        self::KANJIA_STATE_CLOSE        => '活动关闭',
        self::KANJIA_STATE_DELETE_ADMIN => '管理员删除',
        self::KANJIA_STATE_SOFT_DELETE  => '删除'
    );

    private $name_change_state = array(
        'unreviewed'   => 0,
        'pass'         => 1,
        'fail'         => 2,
        'reapply'      => 3,
        'close'        => 4,
        'admin_delete' => 5,
        'soft_delete'  => 6
    );

    public function __construct()
    {
        parent::__construct('p_kanjia_goods');

    }
    /**
     * 新增数据
     *
     * @param array $insert 数据
     * @param string $table 表名
     */
    public function addKanjiaGoods($insert) {
        $result = $this->table('p_kanjia_goods')->insert($insert);
        return $result;
    }
    public function getNameChangeState()
    {
        return $this->name_change_state;
    }

    /**
     * 得到商家是否报名该活动
     * @param  int  $store_id 商家id
     * @param  array  $marketing_ids 活动id
     * @return array
     */
    public function getMerchantApplyState($store_id, $marketing_ids = array())
    {
        $result_data = array();
        if (empty($store_id) || empty($marketing_ids)) return $result_data;

        $field = 'marketing_activity_id,count(kanjia_goods_id) as apply_sum';
        $map = array(
            'marketing_activity_id' => array('in', $marketing_ids),
            'store_id' => (int)$store_id
        );
        $result_data = $this->table('p_kanjia_goods')->field($field)->where($map)->group('marketing_activity_id')->select();
        return array_column($result_data, 'apply_sum', 'marketing_activity_id');
    }
    /**
     * 得到营销活动已报名商品数量    -- 营销活动必要函数
     * @param  integer $kanjia_id 砍价活动编号
     * @param  integer $store_id 店铺id
     * @return int
     */
    public function  getActivityApplyGoodsCount($kanjia_id,$store_id){
        $condition=array(
            "kanjia_id" => $kanjia_id,
            "store_id" => $store_id
        );
        return $this->table('p_kanjia_goods')->where($condition)->count();

    }
    /**
     * 获得活动商品数量  -- 营销活动必要函数
     *
     * @param array $condition
     * @param string $field
     * @return int
     */
    public function getActivityGoodsCount($condition) {
        return $this->table('p_kanjia_goods')->where($condition)->count();
    }

    /**
     * 某个活动id下是否存在报名的商品
     * @param  int $marketing_activity_id 活动id
     * @return bool true - 存在 / false - 不存在
     */
    public function ifExistGoods($marketing_activity_id = 0)
    {
        $map = array(
            'marketing_activity_id' => (int)$marketing_activity_id
        );
        $if_exist = $this->getGoodsInfo($map, 'kanjia_goods_id');
        return empty($if_exist) ? false : true;
    }

    /**
     * 某个活动id下是否存在报名通过的商品
     * @param  int $marketing_activity_id 活动id
     * @return bool true - 存在 / false - 不存在
     */
    public function ifExistApplyGoods($marketing_activity_id = 0)
    {
        $map = array(
            'marketing_activity_id' => (int)$marketing_activity_id,
            'kanjia_state' => self::KANJIA_STATE_PASS,
            'end_time'=>array('gt',time())
        );
        $if_exist = $this->getGoodsInfo($map, 'kanjia_goods_id');
        return empty($if_exist) ? false : true;
    }

    /**
     * 获取商品详细信息
     * @param array $condition
     * @param string $field
     * @return array
     */
    public function getGoodsInfo($condition, $field = '*') {
        return $this->table('p_kanjia_goods')->field($field)->where($condition)->find();
    }

    /**
     * 处理商家商品列表
     * @param  integer $marketing_activity_id      营销活动id
     * @param  array  $goods_list     处理商品数据
     * @return array
     */
    public function disposeStoreGoodsSelect($marketing_activity_id, $goods_list = array())
    {
        $goods_list = (array)$goods_list;
        array_walk($goods_list, function(&$item, $keys){
            $apply_conflict = $this->checkApplyConflict($item['goods_id']);
            $item['apply_conflict'] = $apply_conflict;
            $item['apply_conflict_cause'] = ($apply_conflict !== true) ? $this->getErrorInfo() : '';
        });
        return $goods_list;
    }

    /**
     * 检查是否报名冲突
     * @param  integer $goods_id 商品id
     * @param  array $exclude_condition 排除数据
    $exclude_condition = [
    'promotion_goods_id' => $promotion_goods_id,    //促销活动商品id
    'promotion_id'       => $marketing_activity_id, //营销活动编号
    'promotion_type'     => 0,   //促销类型2限时折扣,3秒杀,4闪购,5限时折扣(全商城)
    'amend_check_state'  => 2   //修改的审核状态
    ];
     * @return true - 未冲突 / false - 冲突
     */
    public function checkApplyConflict($goods_id = 0, $exclude_condition = array())
    {
        $result = false;
        $goods_id = (int)$goods_id;
        if (!$goods_id) {
            $this->setErrorInfo('检查冲突时，商品id为空');
            return false;
        }

        //校验数据的状态 -- 默认通过
        $amend_check_state = isset($exclude_condition['amend_check_state']) ? (int)$exclude_condition['amend_check_state'] : self::KANJIA_STATE_PASS;

        //若数据状态是，未通过/管理员删除/软删除 直接返回，无冲突
        if (($amend_check_state === self::KANJIA_STATE_FAIL) || ($amend_check_state === self::KANJIA_STATE_DELETE_ADMIN) || ($amend_check_state === self::KANJIA_STATE_SOFT_DELETE)) return true;


        //检查是否存在，报名多个进行中，且审核通过的限时特惠
        $map = array(
            'goods_id' => (int)$goods_id,
            // 'start_time' => array('lt', TIMESTAMP),
            'end_time' => array('gt', TIMESTAMP)
        );
        if (isset($exclude_condition['promotion_goods_id'])) $map['kanjia_goods_id'] = array('neq', (int)$exclude_condition['promotion_goods_id']);

        $kanjia_conflict_count = $this->getActivityGoodsCount($map);

        $pintuan_conflict_count = Model('p_pintuan_goods')->getActivityGoodsCount($map);
        if ( ($kanjia_conflict_count > 0) || ($pintuan_conflict_count > 0) ) {
            if($kanjia_conflict_count > 0){
                $kanjia_name = $this->getGoodsInfo($map);
                $this->setErrorInfo('该商品同时段已经报名--'.$kanjia_name['kanjia_name']);
            }elseif($pintuan_conflict_count > 0){
                $pintuan_name = Model('p_pintuan_goods')->getGoodsInfo($map);
                $this->setErrorInfo('该商品同时段已经报名--'.$pintuan_name['pintuan_name']);
            }
            return false;
        }


        //检查是否报名多个未结束的促销活动 -- 新人特惠不用校验
        $model_p_time = Model('p_time');
        $map = array(
            'goods_id' => (int)$goods_id,
            'end_time' => array('gt', TIMESTAMP)
        );
        //  判断是否存在排除条件
        if (isset($exclude_condition['promotion_id']) && isset($exclude_condition['promotion_type'])) {
            $map['promotion_id'] =  array('neq', (int)$exclude_condition['promotion_id']);
            $map['promotion_type'] =  array('neq', (int)$exclude_condition['promotion_type']);
        }
        $conflict_data = $model_p_time->getInfo($map);
        if ($conflict_data) {
            $promotion_type = $model_p_time->getPromotionType();
            $promotion_name = isset($promotion_type[$conflict_data['promotion_type']]) ? $promotion_type[$conflict_data['promotion_type']] : 'unknown';
            $this->setErrorInfo('该商品存在还未结束的'.$promotion_name.'促销活动');
            return false;
        }

        $model_default = Model();
        // 新人专场
        $map_newbie = array(
            'goods_id' => $goods_id,
            'end_time' => array('gt', TIMESTAMP)
        );
        $newbie_count = $model_default->table('p_newbie_goods')->where($map_newbie)->count();
        if ($newbie_count > 0) {
            $this->setErrorInfo('该商品同时段已经报名新人专场');
            return false;
        }
        // 团购
        $map_groupbuy = array(
            'goods_id' => $goods_id,
            'end_time' => array('gt', TIMESTAMP)
        );
        $groupbuy_count = $model_default->table('groupbuy')->where($map_groupbuy)->count();
        if ($groupbuy_count > 0) {
            $this->setErrorInfo('该商品同时段已经报名团购');
            return false;
        }
        // 限时折扣
        $map_xianshi = array(
            'goods_id' => $goods_id,
            'end_time' => array('gt', TIMESTAMP)
        );
        $xianshi_count = $model_default->table('p_xianshi_all_goods')->where($map_xianshi)->count();
        if ($xianshi_count > 0) {
            $this->setErrorInfo('该商品同时段已经报名限时折扣');
            return false;
        }

        return true;
    }

    /**
     * 增加营销活动商品数据 -- 营销活动必要函数, 调用需开启事务
     * @param array $add_data 必要数据
     */
    public function addActivityGoodsData($add_data = array())
    {
        $result = false;
        //获得组装活动商品数据，校验必要数据
        $assemble_data = $this->assembleActivityGoodsOperateData($add_data);
        if (!$assemble_data) {
            $this->setErrorInfo($this->getErrorInfo());
            return $result;
        }
        $price_different = bcsub($assemble_data['goods_price'],$add_data['promotion_price'],2) ;
        if( intval($assemble_data['min_num'])*0.01 >  $price_different ){
            $this->setErrorInfo('销售价格与活动价的差额太小,请确保参与者有最小砍价0.01元');
            return $result;
        }
        //检查是否可以报名
        $apply_allow = $this->checkApplyAllow($assemble_data);
        if ($apply_allow !== true) {
            $this->setErrorInfo($this->getErrorInfo());
            return $result;
        }
        /*补充字段*/
        $model_kanjia = Model('p_kanjia');
        $kanjia_data = $model_kanjia->getInfo(array('marketing_activity_id'=>$assemble_data['marketing_activity_id']));

        $assemble_data['kanjia_id'] = $kanjia_data['kanjia_id'];
        $assemble_data['kanjia_price'] = $assemble_data['xianshi_price'];
        unset($assemble_data['fail_cause']);
        //添加限时折扣(全商城)商品
        $add_state = $this->table('p_kanjia_goods')->insert($assemble_data);
        if (!$add_state) {
            $this->setErrorInfo('添加营销活动商品失败');
            return $result;
        }

        //检查商品状态是否为通过
        if (isset($assemble_data['kanjia_state']) && ($assemble_data['kanjia_state'] === self::KANJIA_STATE_PASS)) {
            //添加价格促销时间
            $model_p_time = Model('p_time');
            $add_data_p = array(
                'start_time'      => $assemble_data['start_time'],
                'end_time'        => $assemble_data['end_time'],
                'store_id'        => $assemble_data['store_id'],
                'goods_id'        => $assemble_data['goods_id'],
                'promotion_id'    => $assemble_data['marketing_activity_id'],
                'promotion_price' => $assemble_data['xianshi_price']
            );
            $add_data_p = $model_p_time->addXianshiAll($add_data_p);
            if (!$add_data_p) {
                $this->setErrorInfo('添加价格促销时间失败');
                return $result;
            }
        }
        $result = $add_state;

        return $result;
    }

    /**
     * 检查报名  -- 营销活动必要函数，需在组装数据后，校验
     * @param  array $check_data 检查数据
     * @return bool  true - 可以报名 / false - 不可以报名
     */
    public function checkApplyAllow($check_data = array())
    {
        static $call_time = array();  //该次事务店铺调用次数，解决事务中，多次调用导致商家报名次数错误的问题
        static $the_apply_goods = array();  //该次事务操作的商品

        $result = false;
        //获取营销活动数据
        $model_activity = Model('marketing_activity');
        $marketing_activity_id = isset($check_data['marketing_activity_id']) ? (int)$check_data['marketing_activity_id'] : 0;
        $field = 'marketing_activity_id,marketing_activity_type,marketing_activity_apply_start_date,marketing_activity_apply_end_date,marketing_activity_start_date,marketing_activity_end_date,store_goods_limit';
        $activity_data = $model_activity->getActivityOpenDetails($marketing_activity_id, $field);
        if (empty($activity_data)) {
            $this->setErrorInfo('不存在该开启的活动');
            return $result;
        }

        //检查报名时间
        $admin_apply = isset($check_data['admin_apply']) ? (int)$check_data['admin_apply'] : 0;  //是否是后台申请(0:商家申请 / 1: 后台申请)
        if ($admin_apply !== 1) { //如果是后台报名，不限制报名时间
            if (($activity_data['marketing_activity_apply_start_date'] > TIMESTAMP) || ($activity_data['marketing_activity_apply_end_date'] < TIMESTAMP)) {
                $this->setErrorInfo('抱歉，该活动报名时间为' . date('Y-m-d H:i', $activity_data['marketing_activity_apply_start_date']) . '到' . date('Y-m-d H:i', $activity_data['marketing_activity_apply_end_date']));
                return $result;
            }
        }

        //判断是否存在该在售商品，且存在库存
        $goods_id = isset($check_data['goods_id']) ? (int)$check_data['goods_id'] : 0;
        $goods_data = Model('goods')->getOnlineGoodsInfo($goods_id, 'goods_id,goods_price,goods_storage');
        if (empty($goods_data) || ($goods_data['goods_storage'] < 1)) {
            $this->setErrorInfo("不存在该ID[{$goods_id}]的商品：已下架/库存不足");
            return $result;
        }

        //检查促销价是否合理
        if ($check_data['xianshi_price'] > $check_data['goods_price']) {
            $this->setErrorInfo('限时折扣(全商城)特惠价格不能大于商品原价');
            return $result;
        }

        //判断该活动是否已结束 -- 活动结束不能报名
        if ($activity_data['marketing_activity_end_date'] < TIMESTAMP) {
            $this->setErrorInfo('活动已结束，不能进行报名');
            return $result;
        }

        //检查该次事务中是否多次操作同一商品 -- 该校验必须在（商品检查是否报名其它促销活动）之前
        if (in_array($check_data['goods_id'], $the_apply_goods)) {
            $this->setErrorInfo('抱歉，请不要多次操作同一数据');
            return $result;
        }
        //添加$the_apply_goods 的值
        $the_apply_goods[] = $check_data['goods_id'];

        //检查该商品是否已经报名其它促销活动(1:报名多个进行中，且审核通过的限时折扣(全商城) 2：报名多个未结束的促销活动 - p_time表， 限时折扣(全商城)不检查该步)
        $apply_conflict = $this->checkApplyConflict($check_data['goods_id']);
        if ($apply_conflict !== true) {
            $this->setErrorInfo($this->getErrorInfo());
            return $result;
        }

        //检查商家最多报名商品数
        $store_goods_limit = isset($activity_data['store_goods_limit']) ? (int)$activity_data['store_goods_limit'] : self::STORE_GOODS_LIMIT;
        if ($store_goods_limit !== 0) {  //限制数为0，不检查该条件
            $store_id = isset($check_data['store_id']) ? (int)$check_data['store_id'] : 0;
            $apply_count = $this->getActivityApplyGoodsCount($marketing_activity_id, $store_id);

            // $store_call_time = isset($call_time[$store_id][$marketing_activity_id]) ? $call_time[$store_id][$marketing_activity_id] : 0;    //获取当前商家该活动的调用次数 -- 因为数据库事务隔离级别默认为：可重复读，所以该操作是多余的
            // if ($activity_data['store_goods_limit'] < $apply_count + $store_call_time) {  //该活动，当前商家已经超限
            //     $this->setErrorInfo('该活动，最多只允许报名' . $activity_data['store_goods_limit'] . '件商品，当前报名为' . ($apply_count + $store_call_time));
            //     return $result;
            // }
            // $store_call_time++; //增加该次事务中商店调用次数
            // $call_time[$store_id][$marketing_activity_id] = $store_call_time;   //存放进静态变量

            if ($activity_data['store_goods_limit'] <= $apply_count) {  //该活动，当前商家已经超限
                $this->setErrorInfo('该活动，最多只允许报名' . $activity_data['store_goods_limit'] . '件商品，当前报名为' . $apply_count);
                return $result;
            }
        }
        return true;
    }

    /**
     * 组装活动商品数据 -- 营销活动必要函数
     * @param  array $operate_data 起始必要数据(marketing_activity_id,goods_id,promotion_price,upper_limit,admin_apply,apply_user_id,apply_user_name)
     * @return array - 成功/ false - 失败
     */
    public function assembleActivityGoodsOperateData($operate_data = array())
    {

        $result = false;
        //检查必要数据
        $obj_validate = new Validate();
        $obj_validate->validateparam = array(
            array("input"=>$operate_data['marketing_activity_id'], 'require'=>'true', 'validator'=>'number', 'message'=>'活动编号为必须,且需为integer'),
            array("input"=>$operate_data['goods_id'], 'require'=>'true', 'validator'=>'number', 'message'=>'商品ID为必须,且需为integer'),
            array("input"=>$operate_data['promotion_price'], 'require'=>'true', 'validator'=>'currency', 'message'=>'特惠价格为必须,且需为currencyt'),
            array("input"=>$operate_data['upper_limit'], 'require'=>'true', 'validator'=>'number', 'message'=>'购买上限为必须,且需为integer'),
            array("input"=>$operate_data['admin_apply'], 'validator'=>'number', 'message'=>'是否是后台操作为必须,且需为integer'),
            array("input"=>$operate_data['apply_user_id'], 'require'=>'true', 'validator'=>'number', 'message'=>'操作人ID为必须,且需为integer'),
            array("input"=>$operate_data['apply_user_name'], 'require'=>'true', 'message'=>'操作人用户名为必须')
        );
        $error = $obj_validate->validate();
        if ($error != ''){
            $this->setErrorInfo($error);
            return $result;
        }

        $model_activity = Model('marketing_activity');
        //获得营销活动数据
        $marketing_activity_id = isset($operate_data['marketing_activity_id']) ? (int)$operate_data['marketing_activity_id'] : 0;
        $field = 'marketing_activity_type,marketing_activity_title,marketing_activity_start_date,marketing_activity_end_date,min_num';
        $marketing_activity_data = $model_activity->getActivityOpenDetails($marketing_activity_id, $field);
        if (empty($marketing_activity_data)) {
            $this->setErrorInfo('不存在该开启的活动');
            return $result;
        }

        //获取商品数据
        $model_goods = Model('goods');
        $goods_id = isset($operate_data['goods_id']) ? (int)$operate_data['goods_id'] : 0;
        $goods_data = $model_goods->getOnlineGoodsInfo($goods_id, 'goods_id,goods_name,store_id,store_name,goods_price,gc_id,goods_image');

        //组装数据
        $assemble = array();  //组装后的数据
        $if_recommend = isset($operate_data['if_recommend']) ? (int)$operate_data['if_recommend'] : 0;
        $goods_sort = isset($operate_data['goods_sort']) ? (int)$operate_data['goods_sort'] : 0;
        $fail_cause = isset($operate_data['fail_cause']) ? $operate_data['fail_cause'] : '';
        //是否是后台添加
        $admin_add = isset($operate_data['admin_apply']) && ($operate_data['admin_apply'] == 1);
        $check_state = $admin_add ? self::KANJIA_STATE_PASS : self::KANJIA_STATE_UNREVIEWED;  //审核状态
        $check_time  = $admin_add ? TIMESTAMP : 0;  //审核时间
        $check_admin_id  = $admin_add ? (int)$operate_data['apply_user_id'] : 0;  //审核人id
        $check_admin_name  = $admin_add ? (string)$operate_data['apply_user_name'] : '';  //审核人用户名
        //找出当前商品历史最低价
        $lowest_price = Model('goods_history')->getGoodsHistoryExtremity($goods_id);
        $lowest_price = isset($lowest_price['min_price']) ? $lowest_price['min_price'] : $goods_data['goods_price'];

        $assemble = array(
            'marketing_activity_id'    => $marketing_activity_id,
            'kanjia_name'              => $marketing_activity_data['marketing_activity_title'],
            'min_num'                  => $marketing_activity_data['min_num'],
            'store_id'                 => $goods_data['store_id'],
            'store_name'               => $goods_data['store_name'],
            'goods_id'                 => $goods_id,
            'goods_name'               => $goods_data['goods_name'],
            'goods_price'              => $goods_data['goods_price'],
            'lowest_price'             => $lowest_price,
            'xianshi_price'            => $operate_data['promotion_price'],
            'gc_id'                    => $goods_data['gc_id'],
            'goods_image'              => $goods_data['goods_image'],
            'start_time'               => $marketing_activity_data['marketing_activity_start_date'],
            'end_time'                 => $marketing_activity_data['marketing_activity_end_date'],
            'upper_limit'              => (int)$operate_data['upper_limit'],
            'kanjia_state'            => $check_state,
            'if_recommend'             => $if_recommend,
            'xianshi_goods_sort'       => $goods_sort,
            'remark'               => $fail_cause,
            'admin_apply'              => (int)$operate_data['admin_apply'],
            'apply_user_id'            => (int)$operate_data['apply_user_id'],
            'apply_user_name'          => (string)$operate_data['apply_user_name'],
            'create_time'              => TIMESTAMP,
            'update_time'              => TIMESTAMP,
            'check_time'               => $check_time,
            'check_admin_id'           => $check_admin_id,
            'check_admin_name'         => $check_admin_name
        );
        return $assemble;
    }
    /**
     * 检查是否报名冲突
     * @param  integer $goods_id 商品id
     * @param  array $exclude_condition 排除数据
    $exclude_condition = [
    'promotion_goods_id' => $promotion_goods_id,    //促销活动商品id
    'promotion_id'       => $marketing_activity_id, //营销活动编号
    'promotion_type'     => 0,   //促销类型2限时折扣,3秒杀,4闪购,5限时折扣(全商城)
    'amend_check_state'  => 2   //修改的审核状态
    ];
     * @return true - 未冲突 / false - 冲突
     */
    public function checkApplyAdminConflict($goods_id = 0, $exclude_condition = array())
    {
        $result = false;
        $goods_id = (int)$goods_id;
        if (!$goods_id) {
            $result  = array('code'=>201,'msg'=>'检查冲突时，商品id为空');
            return $result;
        }

        //校验数据的状态 -- 默认通过
        $amend_check_state = isset($exclude_condition['amend_check_state']) ? (int)$exclude_condition['amend_check_state'] : self::KANJIA_STATE_PASS;

        //若数据状态是，未通过/管理员删除/软删除 直接返回，无冲突
        if (($amend_check_state === self::KANJIA_STATE_FAIL) || ($amend_check_state === self::KANJIA_STATE_DELETE_ADMIN) || ($amend_check_state === self::KANJIA_STATE_SOFT_DELETE)) return true;


        //检查是否存在，报名多个进行中，且审核通过的限时特惠
        $map = array(
            'goods_id' => (int)$goods_id,
            // 'start_time' => array('lt', TIMESTAMP),
            'end_time' => array('gt', TIMESTAMP),
        );
        if (isset($exclude_condition['promotion_goods_id'])) $map['kanjia_goods_id'] = array('neq', (int)$exclude_condition['promotion_goods_id']);
        $kanjia_conflict_count = $this->getActivityGoodsCount($map);
        $pintuan_conflict_count = Model('p_pintuan_goods')->getActivityGoodsCount($map);
        if ( ($kanjia_conflict_count > 0) || ($pintuan_conflict_count > 0) ) {
            if($kanjia_conflict_count > 0){
                $pintuan_name = $this->getGoodsInfo($map);
                $result  = array('code'=>201,'msg'=>'该商品同时段已经报名--'.$pintuan_name['pintuan_name']);
                return $result;
            }elseif($pintuan_conflict_count > 0){
                $kanjia_name = Model('p_kanjia_goods')->getGoodsInfo($map);
                $result  = array('code'=>201,'msg'=>'该商品同时段已经报名--'.$kanjia_name['kanjia_name']);
                return $result;
            }
            return false;
        }


        //检查是否报名多个未结束的促销活动 -- 新人特惠不用校验
        $model_p_time = Model('p_time');
        $map = array(
            'goods_id' => (int)$goods_id,
            'end_time' => array('gt', TIMESTAMP)
        );
        //  判断是否存在排除条件
        if (isset($exclude_condition['promotion_id']) && isset($exclude_condition['promotion_type'])) {
            $map['promotion_id'] =  array('neq', (int)$exclude_condition['promotion_id']);
            $map['promotion_type'] =  array('neq', (int)$exclude_condition['promotion_type']);
        }
        $conflict_data = $model_p_time->getInfo($map);
        if ($conflict_data) {
            $promotion_type = $model_p_time->getPromotionType();
            $promotion_name = isset($promotion_type[$conflict_data['promotion_type']]) ? $promotion_type[$conflict_data['promotion_type']] : 'unknown';
            $result  = array('code'=>201,'msg'=>'该商品存在还未结束的'.$promotion_name.'促销活动');
            return $result;
        }

        $model_default = Model();
        // 新人专场
        $map_newbie = array(
            'goods_id' => $goods_id,
            'end_time' => array('gt', TIMESTAMP)
        );
        $newbie_count = $model_default->table('p_newbie_goods')->where($map_newbie)->count();
        if ($newbie_count > 0) {
            $result  = array('code'=>201,'msg'=>'该商品同时段已经报名新人专场');
            return $result;
        }
        // 团购
        $map_groupbuy = array(
            'goods_id' => $goods_id,
            'end_time' => array('gt', TIMESTAMP)
        );
        $groupbuy_count = $model_default->table('groupbuy')->where($map_groupbuy)->count();
        if ($groupbuy_count > 0) {
            $result  = array('code'=>201,'msg'=>'该商品同时段已经报名团购');
            return $result;
        }
        // 限时折扣
        $map_xianshi = array(
            'goods_id' => $goods_id,
            'end_time' => array('gt', TIMESTAMP)
        );
        $xianshi_count = $model_default->table('p_xianshi_all_goods')->where($map_xianshi)->count();
        if ($xianshi_count > 0) {
            $result  = array('code'=>201,'msg'=>'该商品同时段已经报名限时折扣');
            return $result;
        }

        return true;
    }

    /*删除拼团产品*/
    public function del($condition = array()){
        return $this->where($condition)->delete();
    }
}
